package com.dctp.common.db;

import java.math.BigDecimal;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class UserDb {
    public static Record findByEmail(String email) {
        return Db.findFirst("select * from dctp_user where email = ? ",email);
    }



    public static Long findIdByPromoteCode(Integer promoteCode) {
        return Db.queryLong("select id from dctp_user where promote_code = ? ",promoteCode);
    }


    public static Integer updateField(String key,Object value,Long id) {
        String sql = String.format("update dctp_user set %s = ? where id = ? ",key);
        return Db.update(sql,value,id);
    }


    public static BigDecimal getBalance(Long userId) {
        Record record = Db.findFirst("select balance from dctp_user where id = ?", userId);
        if (record != null) {
            return record.getBigDecimal("balance");
        }
        return BigDecimal.ZERO;
    }
    public static Record findBasicById(Long userId) {
        return Db.findFirst("SELECT id, email, avatar,parent_id,wallet_address,nick_name,tags,promote_code,client_version, role_id,balance,balance2,simulate_balance,commission_balance,top_role_user_id,middle_role_user_id,bottom_role_user_id FROM dctp_user WHERE id = ?", userId);
    }


    public static List<Record> findAll() {
        return Db.find("SELECT id, email, avatar, nick_name, role_id, balance FROM dctp_user where status = 1 and balance > 0 ");
    }
}
